﻿using UnityEngine;
using System.Collections;

public class PlayerCamera : MonoBehaviour {

	// Use this for initialization
	public static GameObject player;
	void Start () 
	{
		player = GameObject.Find("Player");
	}
	
	// Update is called once per frame
	void Update () 
	{
		if (Input.GetMouseButtonDown(0)) 
		{
			if(player.rigidbody2D.gravityScale == 0)
			{
				player.rigidbody2D.gravityScale = GameSettings.GRAVITY_SCALE_PLAYER;
			}

			Vector3 screenPositionPlayer =  Camera.main.WorldToScreenPoint(player.transform.localPosition);
			Vector3 mousePostion = Input.mousePosition;
			if(mousePostion.x < screenPositionPlayer.x)
			{
				//left
				player.rigidbody2D.velocity = Vector2.zero;
				player.rigidbody2D.AddForce(new Vector2(-GameSettings.FORCE_X,GameSettings.FORCE_Y));
			}
			else
			{
				//right
				player.rigidbody2D.velocity = Vector2.zero;
				player.rigidbody2D.AddForce(new Vector2(GameSettings.FORCE_X,GameSettings.FORCE_Y));
			}
		}

		Vector3 lastCameraPostion = this.camera.transform.localPosition;

		if(player.transform.localPosition.y > lastCameraPostion.y)
		{
			this.camera.transform.localPosition = new Vector3(this.camera.transform.localPosition.x,player.transform.localPosition.y,this.camera.transform.localPosition.z);
		}

		this.camera.transform.Translate(new Vector3(0,GameSettings.TEMP_CAMERA_MOVEMENT_SPEED,0));
	}
}
